package com.itita.weibo.ui;


import java.util.ArrayList;
import java.util.List;

import weibo4andriod.Comment;
import weibo4andriod.DirectMessage;
import weibo4andriod.Status;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.ListView;
import android.widget.Toast;

import com.itita.weibo.manager.IWeiboActivity;
import com.itita.weibo.manager.MainService;
import com.itita.weibo.model.CommentToMeAdapter;
import com.itita.weibo.model.DirectMessagesAdapter;
import com.itita.weibo.model.StatusAdapter;
import com.itita.weibo.model.Task;
import com.itita.weibo.util.WeiboUtil;


public final class MessageActivity extends Activity implements IWeiboActivity {

	public static final int REF_GET_WEIBO_COMM = 1;// 刷新所有评论信息

	public static final int REF_GET_WEIBO_ATME = 2;// 刷新所有提到我的信息

	public static final int REF_GET_WEIBO_MESSAGE = 3;// 刷新所有私信信息
	
	public static final int REF_GET_WEIBO_COMM_USER_ICON = 4;// 刷新所有私信信息
	
	public static final int REF_GET_WEIBO_ATME_USER_ICON = 5;// 刷新所有私信信息
	
	public static final int REF_GET_WEIBO_MESSAGE_USER_ICON = 6;// 刷新所有私信信息
    
	public static  int CHANGE_FLAG = 1; //0 -->@我的, 1-->获取评论, 2--->我的私信
	
	private ListView lv;
     
	public static View process;
	
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		// 取消标题栏
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);

		this.setContentView(R.layout.mes);
		init();
 
		this.lv = (ListView) this.findViewById(R.id.listView);
	    this.process = this.findViewById(R.id.progress4ListMsg);
	    this.process.setVisibility(View.GONE);
	}


	public void init() {
		 
		MainService.addActivity(this);
	}
    
	public synchronized void refresh(Object... param) {
		switch ((Integer) param[0]) {
		case REF_GET_WEIBO_COMM:// 评论列表
			MessageActivity.process.setVisibility(View.GONE);
			if(param[1] == null){
				if(MainService.isVerifySucc){
					WeiboUtil.myToastShow(this, "暂无数据");
				}else{
					WeiboUtil.myToastShow(this, "网络连接失败");
				}
				ArrayList<Comment> cmms = new ArrayList<Comment>();;
				// 定义数据适配器
				CommentToMeAdapter commentToMeAdapter = new CommentToMeAdapter(this, cmms);
				lv.setAdapter(commentToMeAdapter);
				break;
			}
			ArrayList<Comment> cmms = (ArrayList<Comment>)param[1];;
			// 定义数据适配器
			CommentToMeAdapter commentToMeAdapter = new CommentToMeAdapter(this, cmms);
			lv.setAdapter(commentToMeAdapter);
			break;
			
		case REF_GET_WEIBO_ATME:// 提到我的
			MessageActivity.process.setVisibility(View.GONE);
			if(param[1] == null){
				if(MainService.isVerifySucc){
					WeiboUtil.myToastShow(this, "暂无数据");
				}else{
					WeiboUtil.myToastShow(this, "网络连接失败");
				}
				 ArrayList<Status> metionMes = new ArrayList<Status>();
				 StatusAdapter statusAdapter = new StatusAdapter(this, metionMes);
				 lv.setAdapter(statusAdapter);
				break;
			}
			 ArrayList<Status> metionMes = (ArrayList<Status>) param[1];
			 StatusAdapter statusAdapter = new StatusAdapter(this, metionMes);
			 lv.setAdapter(statusAdapter);
			 break;
			
		case REF_GET_WEIBO_MESSAGE:// 私信列表
			MessageActivity.process.setVisibility(View.GONE);
			if(param[1] == null){
				if(MainService.isVerifySucc){
					WeiboUtil.myToastShow(this, "暂无数据");
				}else{
					WeiboUtil.myToastShow(this, "网络连接失败");
				}
				// 定义数据适配器
				ArrayList<DirectMessage> alldm = new ArrayList<DirectMessage>();
				DirectMessagesAdapter dma = new DirectMessagesAdapter(this, alldm);
				lv.setAdapter(dma);
				break;
			}
			ArrayList<DirectMessage> alldm = (ArrayList<DirectMessage>) param[1];
			// 定义数据适配器
			DirectMessagesAdapter dma = new DirectMessagesAdapter(this, alldm);
			lv.setAdapter(dma);
			break;
			 
			
		case REF_GET_WEIBO_ATME_USER_ICON:
			 if(MessageActivity.CHANGE_FLAG == 0){
				 ((StatusAdapter)lv.getAdapter()).notifyDataSetChanged();
			 }
			break;
			
		case REF_GET_WEIBO_COMM_USER_ICON:
			 if(MessageActivity.CHANGE_FLAG == 1){
				 ((CommentToMeAdapter)lv.getAdapter()).notifyDataSetChanged();
			 }
			break;
			
		case REF_GET_WEIBO_MESSAGE_USER_ICON:
			 if(MessageActivity.CHANGE_FLAG == 2){
				((DirectMessagesAdapter)lv.getAdapter()).notifyDataSetChanged();
			 }
			
			break;
		}
	}

	// 提到我的微博列表
	public static void getAllMetionMe() {
		MessageActivity.CHANGE_FLAG = 0;
		Task tsGetMetionMe = new Task(Task.TASK_GET_METION_ME, null);
		MainService.newTaskToTaskQueneHead(tsGetMetionMe);
	}
	// 获取所有的评论信息
	public static void getAllComment() {
		MessageActivity.CHANGE_FLAG = 1;
		Task tsGetComm = new Task(Task.TASK_GET_WEIBO_COMMENT, null);
		MainService.newTaskToTaskQueneHead(tsGetComm);
	}

	// 获取所有私信
	public static void getAlldirectMessage() {
		MessageActivity.CHANGE_FLAG = 2;
		// 获取所有的评论信息
		Task tsGetComm = new Task(Task.TASK_GET_WEIBO_MESSAGE, null);
		MainService.newTaskToTaskQueneHead(tsGetComm);
	}

	@Override
	public void initControlsAndAddControlsEvent() {
		// TODO Auto-generated method stub
		
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		Log.d("keydown", ".................." + keyCode);
		if (keyCode == 4)// 如果用户按下了返回键
		{
			MainService.promptExitApp(this);
		}
		return true;
	}


}
 
